package com.halo.service;

import java.io.UnsupportedEncodingException;
import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;
import java.sql.Timestamp;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;

import javax.naming.Context;
import javax.naming.InitialContext;
import javax.naming.NamingException;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.sql.DataSource;

import org.springframework.stereotype.Service;

import com.halo.pojo.OnlineWord;
import com.zhuozhengsoft.pageoffice.FileSaver;

@Service
public class PoService {

	
	public void save(HttpServletRequest request,HttpServletResponse response) throws SQLException, UnsupportedEncodingException{
		//定义保存对象
		FileSaver fs = new FileSaver(request, response);
		Connection conn = getConn();
		Statement stmt = conn.createStatement();
		ResultSet rs = stmt.executeQuery("select Max(ID) from word");
		int newID = 1;
		if (rs.next() && rs.getString(1)!=null) {
			newID = Integer.parseInt(rs.getString(1)) + 1;
		}
		rs.close();
		stmt.close();
		
		String FileSubject = fs.getFormField("FileSubject").trim();
		String fileName = "7oa-" + newID + ".doc"; 
		String getFile = (String) request.getParameter("FileSubject");
		if (getFile != null && getFile.length() > 0)
			FileSubject = new String(getFile.getBytes("iso-8859-1"));
		//out.print(FileSubject);
		SimpleDateFormat df = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");//设置日期格式
		// new Date()为获取当前系统时间
		
		Date d = new Date();
		
		PreparedStatement ps = conn.prepareStatement("Insert into word(ID,FileName,Subject,SubmitTime,SUBMITDATE) values(?,?,?,?,?)");
		ps.setLong(1, newID);
		ps.setString(2, fileName);
		ps.setString(3, FileSubject);
		ps.setString(4, df.format(d));
		ps.setTimestamp(5, new java.sql.Timestamp(d.getTime()));
		ps.execute();
		conn.close();
		
		//保存文件
		fs.saveToFile("D:/opt/zzword/" + fileName);
		//设置保存结果
		fs.setCustomSaveResult("ok");
		//fs.showPage(300,300);
		fs.close();
		
	}
	
	public List<OnlineWord> list() throws SQLException{
		Connection conn = getConn();
		Statement stmt = conn.createStatement();
		ResultSet rs = stmt.executeQuery("select * from word");
		List<OnlineWord> wordList = new ArrayList<OnlineWord>();
		while(rs.next() && rs.getString(1)!=null) {
			OnlineWord ow = new OnlineWord();
			ow.setId(rs.getLong(1));
			ow.setFileName(rs.getString(2));
			ow.setSubject(rs.getString(3));
			ow.setSubmitTime(rs.getString(4));
			ow.setSubmitDate(rs.getDate(5));
			wordList.add(ow);
		}
		rs.close();
		stmt.close();
		return wordList;
	}
	
	private Connection getConn(){
	     Connection con = null;
         try {
             Context ic = new InitialContext();
             DataSource source = (DataSource)ic.lookup("java:comp/env/jdbc/winter");          
            con = source.getConnection();       
        } catch (NamingException e) {
             System.out.println("数据源没找到！");
            e.printStackTrace();
         } catch (SQLException e) {
             System.out.println("获取数连接对象失败！");
             e.printStackTrace();
         }
         return con;
	}
}
